package org.huel.backend.controller;

import org.huel.backend.domain.entity.UserRecommendation;
import org.huel.backend.service.impl.UserRecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户推荐控制器
 */
@RestController
@RequestMapping("/api/user-recommendation")
public class UserRecommendationController {

    @Autowired
    private UserRecommendationService userRecommendationService;

    /**
     * 根据学生ID获取推荐课程
     * @param requestData 请求数据，包含studentId和limit
     * @return 响应结果
     */
    @PostMapping("/by-student")
    public Map<String, Object> getRecommendationsByStudentId(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            String studentId = (String) requestData.get("studentId");
            Integer limit = requestData.get("limit") != null ? (Integer) requestData.get("limit") : 10;

            if (studentId == null || studentId.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "学生ID不能为空");
                return result;
            }

            List<UserRecommendation> recommendations = userRecommendationService.getRecommendationsByStudentId(studentId, limit);
            result.put("success", true);
            result.put("data", recommendations);
            result.put("message", "获取成功");
            result.put("timestamp", java.time.LocalDateTime.now().toString());
            result.put("user", "hoaixanghao");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now().toString());
        }
        return result;
    }

    /**
     * 根据推荐类型获取推荐结果
     * @param requestData 请求数据，包含recommendationType和limit
     * @return 响应结果
     */
    @PostMapping("/by-type")
    public Map<String, Object> getRecommendationsByType(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            String recommendationType = (String) requestData.get("recommendationType");
            Integer limit = requestData.get("limit") != null ? (Integer) requestData.get("limit") : 10;

            if (recommendationType == null || recommendationType.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "推荐类型不能为空");
                return result;
            }

            List<UserRecommendation> recommendations = userRecommendationService.getRecommendationsByType(recommendationType, limit);
            result.put("success", true);
            result.put("data", recommendations);
            result.put("message", "获取成功");
            result.put("timestamp", java.time.LocalDateTime.now().toString());
            result.put("user", "hoaixanghao");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now().toString());
        }
        return result;
    }

    /**
     * 获取所有学生ID列表
     * @param requestData 请求数据
     * @return 响应结果
     */
    @PostMapping("/students")
    public Map<String, Object> getAllStudentIds(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> studentIds = userRecommendationService.getAllStudentIds();
            result.put("success", true);
            result.put("data", studentIds);
            result.put("message", "获取成功");
            result.put("timestamp", java.time.LocalDateTime.now().toString());
            result.put("user", "hoaixanghao");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now().toString());
        }
        return result;
    }

    /**
     * 获取所有推荐类型
     * @param requestData 请求数据
     * @return 响应结果
     */
    @PostMapping("/types")
    public Map<String, Object> getAllRecommendationTypes(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> types = userRecommendationService.getAllRecommendationTypes();
            result.put("success", true);
            result.put("data", types);
            result.put("message", "获取成功");
            result.put("timestamp", java.time.LocalDateTime.now().toString());
            result.put("user", "hoaixanghao");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now().toString());
        }
        return result;
    }

    /**
     * 获取热门推荐课程
     * @param requestData 请求数据，包含limit
     * @return 响应结果
     */
    @PostMapping("/popular-courses")
    public Map<String, Object> getPopularCourses(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer limit = requestData != null && requestData.get("limit") != null ?
                    (Integer) requestData.get("limit") : 6;

            List<UserRecommendation> popularCourses = userRecommendationService.getPopularCourses(limit);
            result.put("success", true);
            result.put("data", popularCourses);
            result.put("message", "获取成功");
            result.put("timestamp", java.time.LocalDateTime.now().toString());
            result.put("user", "hoaixanghao");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now().toString());
        }
        return result;
    }


}